﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using TaskLogger.Properties;
using System.Diagnostics;

namespace TaskLogger
{
    public partial class TrayForm : Form
    {
        public enum States
        {
            Hidden,
            Appearing,
            Visible,
            Disappearing
        }

        private States state;
        public States State
        {
            get
            {
                return state;
            }
            set
            {
                state = value;
            }
        }

        private Console console;

        public TrayForm()
        {
            InitializeComponent();
            console = new Console();
            TransparencyKey = Color.FromArgb(0xFF, 0, 0xFF);
            BackgroundImage = Properties.Resources.traySkin;
            console.TimingStarted += new EventHandler(console_TimingStarted);
            console.TimingStopped += new EventHandler(console_TimingStopped);
            timerPopup.Tick += new EventHandler(timerPopup_Tick);
            timerWaitForUser.Tick += new EventHandler(timerWaitForUser_Tick);
        }

        void timerWaitForUser_Tick(object sender, EventArgs e)
        {
            console.StopTiming();
            notifyIcon.BalloonTipTitle = "Tracking stopped.";
            notifyIcon.BalloonTipText = "Time tracking was stopped, because you did not respond!";
            notifyIcon.BalloonTipIcon = ToolTipIcon.Warning;
            notifyIcon.ShowBalloonTip(60000);
            timerWaitForUser.Stop();
            PopDown();
        }

        void timerPopup_Tick(object sender, EventArgs e)
        {
            timerPopup.Stop();
            PopUp();
            timerWaitForUser.Interval = int.Parse(Properties.Settings.Default.PopUpDuration)*1000;
            timerWaitForUser.Start();
        }

        void console_TimingStopped(object sender, EventArgs e)
        {
            notifyIcon.Icon = Properties.Resources.timerRed;
            timerWaitForUser.Stop();
        }

        void console_TimingStarted(object sender, EventArgs e)
        {
            notifyIcon.Icon = Properties.Resources.timer;
            PopDown();
            timerPopup.Interval = int.Parse(Properties.Settings.Default.PopUpInterval) * 60000;
            timerPopup.Start();
            timerWaitForUser.Stop();
        }

        private void notifyIcon_BalloonTipShown(object sender, EventArgs e)
        {
  
        }

        private void notifyIcon_MouseMove(object sender, MouseEventArgs e)
        {
            
        }

        private void TrayForm_Load(object sender, EventArgs e)
        {
            WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
            Top = LowerLimit;
            Left = WorkAreaRectangle.Right - this.Width;
        }

        protected Rectangle WorkAreaRectangle;

       
        public int UpperLimit
        {
            get
            {
                WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
                return WorkAreaRectangle.Bottom - this.Height;
            }
        }

        public int LowerLimit
        {
            get
            {
                WorkAreaRectangle = Screen.GetBounds(WorkAreaRectangle);
                return WorkAreaRectangle.Bottom;
            }
        }

        private void TrayForm_Paint(object sender, PaintEventArgs e)
        {
        }

        protected Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
            }
            int height = bitmap.Height;
            int width = bitmap.Width;
            GraphicsPath path = new GraphicsPath();
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (bitmap.GetPixel(j, i) != transparencyColor)
                    {
                        int x = j;
                        while ((j < width) && (bitmap.GetPixel(j, i) != transparencyColor))
                        {
                            j++;
                        }
                        path.AddRectangle(new Rectangle(x, i, j - x, 1));
                    }
                }
            }
            Region region = new Region(path);
            path.Dispose();
            return region;
        }


        public Bitmap CloseBitmap;

        private void TrayForm_BackgroundImageChanged(object sender, EventArgs e)
        {
            base.Region = BitmapToRegion((Bitmap) BackgroundImage, TransparencyKey);
          
        }


        private void timerAnimate_Tick(object sender, EventArgs e)
        {
            switch (State)
            {
                case (States.Appearing):
                    {
                        if (Top > UpperLimit)
                            Top -= 5;
                        else
                        {
                            State = States.Visible;
                            timerAnimate.Stop();
                        }
                        break;
                    }
                case (States.Disappearing):
                    {
                        if (Top < LowerLimit)
                            Top += 5;
                        else
                        {
                            State = States.Hidden;
                            timerAnimate.Stop();
                            PopDown();
                        }
                        break;
                    }
            }
        }

        public void PopUp()
        {
            buttonTask.Text = console.SelectedTask;
            buttonContinue.Invalidate();
            buttonStop.Invalidate();
            Show();
            BringToFront();
            if (State == States.Visible)
                return;
            if (State == States.Appearing)
                return;
            Top = LowerLimit;
            State = States.Appearing;
            timerAnimate.Start();
        }

        public void PopDown()
        {
            if (State == States.Hidden)
                return;
            if (State == States.Disappearing)
                return;
            Top = UpperLimit;
            State = States.Disappearing;
            timerAnimate.Start();
        }

        private void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            PopDown();
        }

        private void notifyIcon_Click(object sender, EventArgs e)
        {
            if (State == States.Visible)
                PopDown();
            else if (console.BusyTiming)
                PopUp();
            else
            {
                console.Show();
                console.BringToFront();
            }
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            console.Show();
            console.BringToFront();
        }

        private void toolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripMenuItemOpen_Click(object sender, EventArgs e)
        {
            PopUp();
        }

        private void buttonContinue_Click(object sender, EventArgs e)
        {
            PopDown();
            timerPopup.Interval = int.Parse(Properties.Settings.Default.PopUpInterval) * 60000;
            timerPopup.Start();
            timerWaitForUser.Stop();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            console.StopTiming();
            PopDown();
        }

        private void buttonTask_Click(object sender, EventArgs e)
        {
            console.StopTiming();
            console.Show();
            console.BringToFront();
        }



    }
}
